import sys
from subprocess import Popen, PIPE
import os

print "Python based depends utility where python calls g++"
file_list = os.listdir(os.curdir)
test_file_list = os.listdir(os.curdir + "/tests")
src_list = [item for item in file_list if item[-4:] == ".cpp"]
test_src_list = ["tests/" + item for item in test_file_list if item[-4:] == ".cpp"]
print src_list
print test_src_list
print

outdir = sys.argv[1]
print "output directory :", outdir 

mycmd = ["g++", "-MM"] + sys.argv[2:]
# print mycmd + [src_list[0]]
data = ''
for src in src_list:
	output = Popen(mycmd + [src], stdout=PIPE).communicate()[0]
	data += outdir + "/" + output.replace(" \\\r\n", "").replace("\r\n","\n")
print data

for src in test_src_list:
	output = Popen(mycmd + [src], stdout=PIPE).communicate()[0]
	data += r"tests\buildtests_"+os.environ["version"]+"/" + output.replace(" \\\r\n", "").replace("\r\n","\n")
print data

f= file(outdir+r"/vector.d", "w+")
if os.environ["version"][:2] == "VC":
	data = data.replace(".o", ".obj")
f.write(data)
f.close()
